
<!DOCTYPE html>
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
		<style>
			body {
				background: black;
				color: rgb(80, 80, 80);
			}
			body, pre, #legend span {
				font-family: Menlo, monospace;
				font-weight: bold;
			}
			#topbar {
				background: black;
				position: fixed;
				top: 0; left: 0; right: 0;
				height: 42px;
				border-bottom: 1px solid rgb(80, 80, 80);
			}
			#content {
				margin-top: 50px;
			}
			#nav, #legend {
				float: left;
				margin-left: 10px;
			}
			#legend {
				margin-top: 12px;
			}
			#nav {
				margin-top: 10px;
			}
			#legend span {
				margin: 0 5px;
			}
			.cov0 { color: rgb(192, 0, 0) }
.cov1 { color: rgb(128, 128, 128) }
.cov2 { color: rgb(116, 140, 131) }
.cov3 { color: rgb(104, 152, 134) }
.cov4 { color: rgb(92, 164, 137) }
.cov5 { color: rgb(80, 176, 140) }
.cov6 { color: rgb(68, 188, 143) }
.cov7 { color: rgb(56, 200, 146) }
.cov8 { color: rgb(44, 212, 149) }
.cov9 { color: rgb(32, 224, 152) }
.cov10 { color: rgb(20, 236, 155) }

		</style>
	</head>
	<body>
		<div id="topbar">
			<div id="nav">
				<select id="files">
				
				<option value="file0">gitee.com\shu2015626\go_start_imooc\chapter03-container\nonrepeatingsubstr\norepeating.go (52.6%)</option>
				
				<option value="file1">gitee.com\shu2015626\go_start_imooc\chapter03-container\nonrepeatingsubstr\norepeating_self.go (0.0%)</option>
				
				</select>
			</div>
			<div id="legend">
				<span>not tracked</span>
			
				<span class="cov0">not covered</span>
				<span class="cov8">covered</span>
			
			</div>
		</div>
		<div id="content">
		
		<pre class="file" id="file0" style="display: none">package main

import (
        "fmt"
)

//https://leetcode-cn.com/problems/longest-substring-without-repeating-characters/
//寻找最长不含有重复字符的子串
//abcabcbb -&gt; abc
//bbbbbbb -&gt; b
//pwwkew -&gt; wke


func lengthOfLongestSubstring(s string) int <span class="cov8" title="1">{
        lastOccurred := make(map[rune] int)
        start := 0
        maxLength :=0

        //为了支持中文，这里不能使用[]byte(s),而应该使用[]rune(s)
        for i, ch := range []rune(s)</span><span class="cov8" title="1">{
                //fmt.Printf("i=%d, ch=%s\n", i, string(ch))
                if lastI, ok := lastOccurred[ch];ok &amp;&amp; lastI &gt;= start </span><span class="cov8" title="1">{
                        start = lastI + 1
                        //fmt.Printf("===》 start=%d, lastI=%d\n", start, lastI)
                }</span>
                <span class="cov8" title="1">if i-start+1 &gt; maxLength</span><span class="cov8" title="1">{
                        maxLength = i - start + 1
                        //fmt.Printf("===》 start=%d, maxLength=%d\n", start, maxLength)
                }</span>
                <span class="cov8" title="1">lastOccurred[ch] = i</span>
                //fmt.Println("===》 lastOccurred=%v", lastOccurred)
        }
        <span class="cov8" title="1">return maxLength</span>
}

func main() <span class="cov0" title="0">{

        fmt.Println(lengthOfLongestSubstring("abcabcbb"))
        fmt.Println(lengthOfLongestSubstring("bbbbb"))
        fmt.Println(lengthOfLongestSubstring("pwwkew"))

        fmt.Println(lengthOfLongestSubstring(""))
        fmt.Println(lengthOfLongestSubstring("b"))
        fmt.Println(lengthOfLongestSubstring("abcdef"))

        fmt.Println(lengthOfLongestSubstring("我爱慕课网"))
        fmt.Println(lengthOfLongestSubstring("一二三二一"))
        fmt.Println(
                lengthOfLongestSubstring("黑化肥挥发发灰会花飞灰化肥挥发发黑会飞花"),
        )
}</pre>
		
		<pre class="file" id="file1" style="display: none">package main

import (
        "strings"
)

//https://leetcode-cn.com/problems/longest-substring-without-repeating-characters/
//寻找最长不含有重复字符的子串
//abcabcbb -&gt; abc
//bbbbbbb -&gt; b
//pwwkew -&gt; wke


//错的，例如：dvdf
func lengthOfLongestSubstring_self(s string) map[string]int <span class="cov0" title="0">{
        d := make(map[string]int)
        var k string

        for _, c := range s</span><span class="cov0" title="0">{
                if strings.Contains(k, string(c))</span><span class="cov0" title="0">{
                        k = string(c)
                        d[k] = len(k)
                }</span><span class="cov0" title="0">else{
                        k += string(c)
                        d[k] = len(k)
                }</span>
        }
        <span class="cov0" title="0">return d</span>
}

//func main() {
//        s := "abcabcbb"
//        d := lengthOfLongestSubstring_self(s)
//        fmt.Println(d)
//}</pre>
		
		</div>
	</body>
	<script>
	(function() {
		var files = document.getElementById('files');
		var visible;
		files.addEventListener('change', onChange, false);
		function select(part) {
			if (visible)
				visible.style.display = 'none';
			visible = document.getElementById(part);
			if (!visible)
				return;
			files.value = part;
			visible.style.display = 'block';
			location.hash = part;
		}
		function onChange() {
			select(files.value);
			window.scrollTo(0, 0);
		}
		if (location.hash != "") {
			select(location.hash.substr(1));
		}
		if (!visible) {
			select("file0");
		}
	})();
	</script>
</html>
